package com.nursinghome.common.service.impl;

import com.nursinghome.bean.OrderDetail;
import com.nursinghome.common.mapper.OrderDetailMapper;
import com.nursinghome.common.service.OrderDetailService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 程硕
* @createDate 2025-04-07 01:10:33
*/
@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Override
    public Boolean insert(OrderDetail orderDetail) throws NurisingHomeException {
        return orderDetailMapper.insert(orderDetail) > 0;
    }

    @Override
    public Boolean update(OrderDetail orderDetail) throws NurisingHomeException {
        return orderDetailMapper.update(orderDetail) > 0;
    }

    @Override
    public Boolean deleteByOrderId(String orderId) throws NurisingHomeException {
        // 查询所有的订单详情
        List<OrderDetail> orderDetails = orderDetailMapper.selectByOid(orderId);
        for (OrderDetail orderDetail : orderDetails) {
            if (orderDetailMapper.delete(orderDetail.getId()) <= 0) {
                throw new NurisingHomeException("订单详情删除失败");
            }
        }
        return true;
    }


    @Override
    public OrderDetail selectById(Long id) {
        return orderDetailMapper.selectById(id);
    }

    @Override
    public List<OrderDetail> selectAll() {
        return orderDetailMapper.selectAll(new OrderDetail());
    }
}
